package com.cgbl.cg.vm.course

import android.app.Application
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.CourseBean
import com.robot.base.model.PageListBean
import com.robot.base.model.PreferentPageBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class ExcellentCourseViewModel(application: Application) : GLBaseViewModel(application) {
    val filterName = ObservableField("全部")
    var filterType = 0
    val list = arrayListOf<String>()
    val type = ObservableField<Int>(0)

    val tag = ObservableField(true)
    val uc = UiChangeEvent()

    var pageNo = 1

    class UiChangeEvent {
        val showFilter: SingleLiveEvent<ArrayList<String>> = SingleLiveEvent()
        val refreshEvent = SingleLiveEvent<Int>()
        val refreshDataEvent = SingleLiveEvent<List<CourseBean>>()
        val refreshLimitedTimeDataEvent = SingleLiveEvent<List<PreferentPageBean>>()
    }

    val leftTagCommand = BindingCommand<Any>(BindingAction {
        tag.set(true)
        refresh()
    })

    val rightTagCommand = BindingCommand<Any>(BindingAction {
        tag.set(false)
        refresh()
    })

    val filterCommand = BindingCommand<Any>(BindingAction {
        uc.showFilter.call()
    })

    fun initData() {
        list.addAll(arrayListOf("全部", "免费专区", "付费专区", "VIP限免"))
        refresh()
    }

    fun refresh() {
        pageNo = 1
        page()
    }

    fun load() {
        pageNo++
        page()
    }

    fun page() {
        if (type.get() != 3) {
            val map = hashMapOf<String, Any>(
                "pageNo" to pageNo,
                "pageSize" to 10,
                "sortType" to if (tag.get() == true) {
                    0
                } else {
                    1
                },
                "isAccount" to 0,
            )
            when (filterType) {
                1 -> {
                    map["isFree"] = 1
                }
                2 -> {
                    map["isFree"] = 0
                }
                3 -> {
                    map["isFree"] = 2
                }
            }
            when (type.get()) {
                0 -> {
                    map["classRecommend"] = 1
                }
                1 -> {
                    map["classRecommend"] = 1
                }
                2 -> {
                    map["classRecommend"] = 1
                }
            }
            CourseServiceFactory.page(
                map
            ).subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<PageListBean<CourseBean>>>(this) {
                override fun onNext(t: BaseResponse<PageListBean<CourseBean>>) {
                    uc.refreshDataEvent.postValue(t.data.list)
                    uc.refreshEvent.postValue(
                        if (t.data.total.toInt() < pageNo * 10) {
                            0
                        } else {
                            1
                        }
                    )
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    uc.refreshEvent.postValue(
                        0
                    )
                }

            })
        } else {
            UserServiceFactory.getPreferentPage(
                mapOf(
                    "pageNo" to pageNo,
                    "pageSize" to 10,
                    "sort" to if (tag.get() == true) {
                        "0"
                    } else {
                        "1"
                    },
                    "projectType" to 0
                )
            ).subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<PageListBean<PreferentPageBean>>>(this) {
                override fun onNext(t: BaseResponse<PageListBean<PreferentPageBean>>) {
                    uc.refreshLimitedTimeDataEvent.postValue(t.data.list)
                    uc.refreshEvent.postValue(
                        if (t.data.total.toInt() < pageNo * 10) {
                            0
                        } else {
                            1
                        }
                    )
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    uc.refreshEvent.postValue(
                        0
                    )
                }
            })
        }
    }
}